// pages/my/receiveLcCoupon.js
import consts from "../../js/const";
import util from "../../utils/util";
import Toast from '@vant/weapp/toast/toast';

Page({

    /**
     * 页面的初始数据
     */
    data: {
        imageUrl: consts.getImageUrl('repair-coupon.png'),
        sumAmount: 0,
        areas: [
            {text:'黄埔区',value:'黄埔区'},
            {text:'徐汇区',value:'徐汇区'},
            {text:'长宁区',value:'长宁区'},
            {text:'静安区',value:'静安区'},
            {text:'普陀区',value:'普陀区'},
            {text:'虹口区',value:'虹口区'},
            {text:'杨浦区',value:'杨浦区'},
            {text:'闵行区',value:'闵行区'},
            {text:'宝山区',value:'宝山区'},
            {text:'嘉定区',value:'嘉定区'},
            {text:'浦东新区',value:'浦东新区'},
            {text:'金山区',value:'金山区'},
            {text:'松江区',value:'松江区'},
            {text:'青浦区',value:'青浦区'},
            {text:'奉贤区',value:'奉贤区'},
            {text:'崇明区',value:'崇明区'}
        ],

        areaTitle: '区域',
        subTypeTitle: '加油券',
        vehicleTypeTitle: '车辆类型',
        params:{typeCde: 'LC_COUPON'}
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let that = this;
        let params=that.data.params;
        that.setData({maxAmount: options.maxAmount},function (){
            that.refreshCouponList(params);
        });

        util.promise('/searchVehicleTypeCategoryItemByTypeCde', params.typeCde).then((res) => {
            if(res.data){
                let vehicleTypes=util.map(res.data,function (item){
                   return {text: item.vehicleTypeNme, value: item.vehicleType};
                })
                that.setData({vehicleTypes:vehicleTypes});
            }
        });
        util.promise('/searchSubTypeCategoryItemByTypeCde', params.typeCde).then((res) => {
            if(res.data){
                let subTypes=util.map(res.data,function (item){
                   return {text: item.subTypeNme , value: item.subTypeCde};
                })
                that.setData({subTypes:subTypes});
            }
        });
    },

    refreshCouponList(params){
        let that=this;
        util.promise('/searchEycCouponRuleByTypeCde',params ).then((res) => {
            if (res.data) {
                res.data.forEach(function (item) {
                    item.qty = 0;
                });
                that.setData({couponList: res.data});
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    isShowRemark(event) {
        let that = this;
        let item = event.currentTarget.dataset.item;
        let couponList = event.currentTarget.dataset.couponList;
        couponList.forEach(function (r) {
            if ((r.eycCouponRuleId === item.eycCouponRuleId && !item.eycCouponId) || (item.eycCouponId && item.eycCouponId === r.eycCouponId)) {
                r.isShowRemark = !r.isShowRemark;
            } else {
                r.isShowRemark = false;
            }
        });
        that.setData({couponList: couponList});
    },
    onChange(event) {
        let that = this;
        let couponList = that.data.couponList;
        let sumAmount = 0;
        let currentCoupon = event.currentTarget.dataset.item;
        couponList.forEach(function (item) {
            if (currentCoupon.eycCouponRuleId === item.eycCouponRuleId) {
                item.qty = event.detail;
            }
            if (item.qty > 0) {
                sumAmount += item.qty * item.amount;
            }

        })
        that.setData({couponList: couponList, sumAmount: sumAmount});
    },
    receiveLcCoupons(event) {
        let that = this;
        if (that.data.sumAmount > that.data.maxAmount) {
            Toast('已选总金额不能大于可选总金额');
            return;
        }
        let couponList = that.data.couponList;
        let coupons = [];
        couponList.forEach(function (item) {
            if (item.qty > 0) {
                for (let i = 0; i < item.qty; i++) {
                    coupons.push(util.copy(item));
                }
            }
        })

        util.promise('/receiveCoupons', coupons).then((res) => {
            if (res.data) {
                const eventChannel = that.getOpenerEventChannel();
                eventChannel.emit('acceptDataFromPage');
                Toast({
                    type: 'success',
                    message: '领取成功',
                    onClose: () => {
                        wx.navigateBack({
                            delta: 1
                        });
                    }
                })
            }
        })
    },
    changeSubType(event){
        let that=this;
        let params=that.data.params;
        params.subTypeCde= event.detail;
       that.setData({params:params},function (){
           that.refreshCouponList(params);
       })
    },
    changeVehicleType(event){
        let that=this;
        let params=that.data.params;
        params.vehicleType= event.detail;
       that.setData({params:params},function (){
           that.refreshCouponList(params);
       })
    },
    changeArea(event){
        let that=this;
        let params=that.data.params;
        params.prepecture= event.detail;
       that.setData({params:params},function (){
           that.refreshCouponList(params);
       })
    }
})